package cn.cosmosx.controller.ocr;

import cn.cosmosx.base.ocr.baidu.BaiduOcr;
import cn.cosmosx.base.ocr.baidu.model.BusinessLicense;
import cn.cosmosx.base.ocr.baidu.model.IdCard;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.cosmosx.base.define.ResultVO;

import javax.validation.constraints.NotNull;

/**
 * 调用百度的文字识别Api
 */
@Api(tags = "OCR识别")
@Slf4j
@RestController
@RequestMapping(value = "/ocr/baidu")
public class OcrController {

    @Autowired
    private BaiduOcr baiduOcr;

    @ApiOperation(value = "身份证正面图片识别")
    @PostMapping("/idCard/front")
    public ResultVO<IdCard> idCardFront(@NotNull @ApiParam(name = "file", value = "身份证正面图片", required = true) @RequestPart(value = "file") MultipartFile file) throws Exception {
        return ResultVO.success(baiduOcr.idCardFront(file));
    }

    @ApiOperation(value = "身份证图片识别")
    @PostMapping("/idCard")
    public ResultVO<IdCard> idCard(@NotNull @ApiParam(name = "file", value = "身份证图片", required = true) @RequestPart(value = "file") MultipartFile file) throws Exception {
        return ResultVO.success(baiduOcr.idCardMulti(file));
    }

    @ApiOperation(value = "营业执照图片识别")
    @PostMapping("/businessLicense")
    public ResultVO<BusinessLicense> businessLicense(@NotNull @ApiParam(name = "file", value = "营业执照图片", required = true) @RequestPart(value = "file") MultipartFile file) throws Exception {
        return ResultVO.success(baiduOcr.businessLicense(file));
    }

    @ApiOperation(value = "增值税发票图片识别-图片格式")
    @PostMapping("/vatInvoiceImage")
    public ResultVO<JSONObject> vatInvoiceImage(@NotNull @ApiParam(name = "file", value = "营业执照图片", required = true) @RequestPart(value = "file") MultipartFile file) throws Exception {
        return ResultVO.success(JSON.parseObject(baiduOcr.vatInvoiceImage(file)));
    }

}

